/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package toolsKit.DAO;

import toolsKit.csv.CsvFile;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javaapplication5.model.Contact;
import javaapplication5.model.SimpleContact;

/**
 *
 * @author mathieubakrim
 */
public abstract class AbstractContactDao implements ContactDao{
    protected CsvFile csvFile;
    
    private Map<String, Contact> contactsMap;
    private List<Contact> contacts;
    private Map<String, List<Contact>> contactsMultimapByNom;
    
    
    protected SimpleContact tabToContact(String[] tab){
        if(tab==null){
            throw new IllegalArgumentException("Table cannot be null");            
        }
        
        final SimpleContact contact = new SimpleContact();
        
        contact.setLastName(tab[0]);
        contact.setFirstName(tab[1]);
        contact.setPhoneNumber(tab[2]);
        contact.setAddressPro(tab[3]);
        contact.setAddressPer(tab[4]);
        contact.setHomePage(tab[5]);
        contact.setEmailPro(tab[6]);
        contact.setEmailPer(tab[7]);
        
        return contact;
    }
    
    protected Contact mapToContact(Map<String, String> map){
        if (map == null){
            throw new IllegalArgumentException("The map cannot be null");
        }
        
        final SimpleContact contact = new SimpleContact();
        
        final String lastName = map.get("lastName");
        final String firstName = map.get("firstName");
        final String phoneNumber = map.get("phoneNumber");
        final String addressPro = map.get("addressPro");
        final String addressPer = map.get("addressPer");
        final String homePage = map.get("homePage");
        final String emailPro = map.get("emailPro");
        final String emailPer = map.get("emailPer");
        
        contact.setLastName(lastName);
        contact.setFirstName(firstName);
        contact.setPhoneNumber(phoneNumber);
        contact.setAddressPro(addressPro);
        contact.setAddressPer(addressPer);
        contact.setHomePage(homePage);
        contact.setEmailPro(emailPro);
        contact.setEmailPer(emailPer);
        
        return contact;
    };
    
    protected List<Contact> findAllContactsByMap(){
        if(contacts == null){
            init();
        }
        return contacts;
    }
    
    public Contact findContactByName(String name){
        if(contactsMap == null){
            init();
        }
        
        return contactsMap.get(name);
    }
    
    
    protected void init(){
        final List<Map<String, String>> mappedData = csvFile.getMappedData();
        
        //Initialization to the right size.
        contacts = new ArrayList<Contact>(mappedData.size());
        contactsMap = new HashMap<String, Contact>(mappedData.size());
        
        // We cannot know multimap size in advance.
        contactsMultimapByNom = new HashMap<String, List<Contact>>();
        
        //Major change here : I replace the type integer by string on the left part of the map
        for(Map<String, String> map : mappedData){
            final Contact contact = mapToContact(map);
            contacts.add(contact);
            contactsMap.put(contact.getLastName(), contact);
            
            
            addContactToMultimap(contact, contactsMultimapByNom, contact.getLastName());
        }
    }
    
    private void addContactToMultimap(Contact contact, Map<String, List<Contact>> multimap, String key){
        List<Contact> sublist = multimap.get(key);
        if(sublist == null){
            sublist = new ArrayList<Contact>();
            multimap.put(key, sublist);
        }
        sublist.add(contact);        
    }    

    
    
}
